﻿using System;
using System.DirectoryServices;

namespace MockingBird.Common.Infrastructure.IIS
{
    public class IISManager
    {
        #region Private Members
        private DirectoryEntry iisServer;
        #endregion

        public static string VirDirSchemaName = "IIsWebVirtualDir";

        public string ServerName { get; set; }


        /// <summary>
        ///		Constructor
        /// </summary>
        /// <param name="serverName">Name of the IIS Server</param>
        public IISManager(string serverName)
        {
            this.ServerName = serverName;
        }
        /// <summary>
        ///		Default constructor uses localhost as default server
        /// </summary>
        public IISManager()
        {
            ServerName = "localhost";
        }

        /// <summary>
        ///		Connect to IISServer
        /// </summary>
        public void Connect()
        {
            try
            {
                this.iisServer = new DirectoryEntry("IIS://" + this.ServerName + "/W3SVC/1");
            }
            catch (Exception e)
            {
                throw new Exception("Could not connect to: " + ServerName, e);
            }
        }

        /// <summary>
        ///		Create a virtual directory
        /// </summary>
        /// <param name="directoryName">Name of the new virtual directory</param>
        /// <param name="realPath">Path of the directory</param>
        public void CreateVirtualDirectory(string directoryName, string realPath)
        {
            DirectoryEntry folderRoot = this.iisServer.Children.Find("Root", VirDirSchemaName);
            try
            {
                DirectoryEntry newVirDir = folderRoot.Children.Add(directoryName, VirDirSchemaName);
                // Set Properties
                newVirDir.Properties["AccessRead"].Add(true);
                newVirDir.Properties["Path"].Add(realPath);
                // Create a Application
                newVirDir.Invoke("AppCreate", true);
                // Save Changes
                newVirDir.CommitChanges();
                folderRoot.CommitChanges();
                this.iisServer.CommitChanges();
            }
            catch (Exception e)
            {
                throw new Exception("Virtual Directory " + directoryName + " could not be created.", e);
            }
        }

        public void DeleteVirtualDirectory(string directoryName)
        {
            DirectoryEntry folderRoot = this.iisServer.Children.Find("Root", VirDirSchemaName);
            try
            {
                DirectoryEntry targetEntry = folderRoot.Children.Find(directoryName);
                targetEntry.DeleteTree();
                folderRoot.CommitChanges();
                this.iisServer.CommitChanges();
            }
            catch (Exception e)
            {
                throw new Exception("Virtual Directory " + directoryName + " could not be deleted.", e);
            }
        }

        public bool DirectoryExists(string directoryName)
        {
            DirectoryEntry folderRoot = this.iisServer.Children.Find("Root", VirDirSchemaName);
            DirectoryEntry targetEntry = folderRoot.Children.Find(directoryName);
            return DirectoryEntry.Exists(targetEntry.Path);

        }



    }
}
